//
//  LeaveMessageTableViewCell.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/13.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "LeaveMessageTableViewCell.h"
#define userImgW 20/WIDTH_6_SCALE
@interface LeaveMessageTableViewCell()
@property (nonatomic, strong)UIButton * userFavourBtn;//用户点赞
@property (nonatomic, strong)UILabel * userNumberLab;//用户点赞数
@property (nonatomic, strong)UILabel * userWordLab;//用户留言内容
@property (nonatomic, strong)UILabel * userDateLab;//用户留言时间
@property (nonatomic, strong)UIImageView * userImg;//用户头像
@property (nonatomic, strong)UIView * grayBGView;//灰色背景
@property (nonatomic, strong)UIButton * officialFavourBtn;//官方回复点赞图像
@property (nonatomic, strong)UILabel * officialNumLab;//官方点赞数
@property (nonatomic, strong)UILabel * officialWordLab;//官方回复内容
@property (nonatomic, strong)UIView * greenLineView;//绿色线
@property (nonatomic, strong)UILabel * officialDateLab;//官方回复时间
@end

@implementation LeaveMessageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];

        [self.contentView addSubview:self.userFavourBtn];
        [self.contentView addSubview:self.userNumberLab];
        [self.contentView addSubview:self.userWordLab];
        [self.contentView addSubview:self.userDateLab];
        [self.contentView addSubview:self.userImg];
        [self.contentView addSubview:self.grayBGView];
        [self.contentView addSubview:self.officialFavourBtn];
        [self.contentView addSubview:self.officialNumLab];
        [self.grayBGView addSubview:self.officialWordLab];
        [self.grayBGView addSubview:self.greenLineView];
        [self.grayBGView addSubview:self.officialDateLab];
        [self.contentView addSubview:self.bottomLineView];
    }return self;
}
- (UIButton *)userFavourBtn{
    if (!_userFavourBtn) {
        _userFavourBtn = [[UIButton alloc] init];
        [_userFavourBtn setImage:[UIImage imageNamed:@"zan_gray_icon"] forState:UIControlStateNormal];
        [_userFavourBtn setImage:[UIImage imageNamed:@"zan_green_icon"] forState:UIControlStateSelected];
        [_userFavourBtn addTarget:self action:@selector(userFavourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }return _userFavourBtn;
}
- (UILabel *)userNumberLab{
    if (!_userNumberLab) {
        _userNumberLab = [[UILabel alloc] init];
        _userNumberLab.font = DEF_FontSize_12;
        _userNumberLab.textColor = getColor(@"9d9d9d");
        _userNumberLab.textAlignment = NSTextAlignmentCenter;
    }return _userNumberLab;
}
- (UILabel *)userWordLab{
    if (!_userWordLab) {
        _userWordLab = [[UILabel alloc] init];
        _userWordLab.font = DEF_FontSize_14;
        _userWordLab.textColor = [UIColor blackColor];
        _userWordLab.textAlignment = NSTextAlignmentLeft;
        _userWordLab.lineBreakMode = 0;
        _userWordLab.numberOfLines = 0;
    }return _userWordLab;
}
- (UILabel *)userDateLab{
    if (!_userDateLab) {
        _userDateLab = [[UILabel alloc] init];
        _userDateLab.font = DEF_FontSize_13;
        _userDateLab.textColor = getColor(@"c3c3c3");
        _userDateLab.textAlignment = NSTextAlignmentLeft;
    }return _userDateLab;
}
- (UIImageView *)userImg{
    if (!_userImg) {
        _userImg = [[UIImageView alloc] init];
        _userImg.image = [UIImage imageNamed:@"head_login_icon"];
    }return _userImg;
}
- (UIView *)grayBGView{
    if (!_grayBGView) {
        _grayBGView = [[UIView alloc] init];
        _grayBGView.backgroundColor = getColor(@"f5f5f5");
    }return _grayBGView;
}
- (UIButton *)officialFavourBtn{
    if (!_officialFavourBtn) {
        _officialFavourBtn = [[UIButton alloc] init];
        [_officialFavourBtn setImage:[UIImage imageNamed:@"zan_gray_icon"] forState:UIControlStateNormal];
        [_officialFavourBtn setImage:[UIImage imageNamed:@"zan_green_icon"] forState:UIControlStateSelected];
        [_officialFavourBtn addTarget:self action:@selector(officialFavourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }return _officialFavourBtn;
}
- (UILabel *)officialNumLab{
    if (!_officialNumLab) {
        _officialNumLab = [[UILabel alloc] init];
        _officialNumLab.font = DEF_FontSize_14;
        _officialNumLab.textColor = getColor(@"9d9d9d");
        _officialNumLab.textAlignment = NSTextAlignmentCenter;
    }return _officialNumLab;
}
- (UILabel *)officialWordLab{
    if (!_officialWordLab) {
        _officialWordLab = [[UILabel alloc] init];
        _officialWordLab.font = DEF_FontSize_14;
        _officialWordLab.textColor = [UIColor blackColor];
        _officialWordLab.textAlignment = NSTextAlignmentLeft;
        _officialWordLab.lineBreakMode = 0;
        _officialWordLab.numberOfLines = 0;
    }return _officialWordLab;
}
- (UIView *)greenLineView{
    if (!_greenLineView) {
        _greenLineView = [[UIView alloc] init];
        _greenLineView.backgroundColor = getColor(greenBGColor);
    }return _greenLineView;
}
- (UILabel *)officialDateLab{
    if (!_officialDateLab) {
        _officialDateLab = [[UILabel alloc] init];
        _officialDateLab.font = DEF_FontSize_14;
        _officialDateLab.textColor = getColor(@"c3c3c3");
        _officialDateLab.textAlignment = NSTextAlignmentLeft;
    }return _officialDateLab;
}
- (UIView *)bottomLineView{
    if (!_bottomLineView) {
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = getColor(@"eeeeee");
    }return _bottomLineView;
}
+ (CGFloat)heightForCellWithComment:(NSDictionary *) model{
    if ([model[@"officialWord"] isEqualToString:@""] || model[@"officialWord"] == nil || [model[@"officialWord"] length] == 0 ) {
        return [self userWordLabelHeight:model[@"userWord"]] + 15/WIDTH_6_SCALE + 43/WIDTH_6_SCALE;
    }else{
        return [self userWordLabelHeight:model[@"userWord"]] + 138/WIDTH_6_SCALE + [self officialWordLabelHeight:model[@"officialWord"]];
    }
}

+ (CGFloat)userWordLabelHeight:(NSString *) text{
    CGRect rectToFit = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 110/WIDTH_6_SCALE, MAXFLOAT)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}
                                          context:nil];
    return rectToFit.size.height;
}
+ (CGFloat)officialWordLabelHeight:(NSString *) text{
    CGRect rectToFit = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 105/WIDTH_6_SCALE, MAXFLOAT)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}
                                          context:nil];
    return rectToFit.size.height;
}
- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.userWordLab.text = model[@"userWord"];
    self.userDateLab.text = model[@"userData"];
    self.officialWordLab.text = model[@"officialWord"];
    self.officialDateLab.text = model[@"officialData"];
    self.userNumberLab.text = model[@"userNum"];
    self.officialNumLab.text = model[@"officialNum"];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    int favourImgW = 15/WIDTH_6_SCALE;
    self.userFavourBtn.frame = CGRectMake(25/WIDTH_6_SCALE, 10/WIDTH_6_SCALE, favourImgW, favourImgW);
    self.userNumberLab.frame = CGRectMake(5/WIDTH_6_SCALE, self.userFavourBtn.bottom + 8/WIDTH_6_SCALE, 33/WIDTH_6_SCALE, 11/WIDTH_6_SCALE);
    self.userNumberLab.centerX = self.userFavourBtn.centerX;
    self.userImg.frame = CGRectMake(SCREEN_WIDTH - 35/WIDTH_6_SCALE, 19/WIDTH_6_SCALE, userImgW, userImgW);
    self.userWordLab.frame = CGRectMake(60/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, SCREEN_WIDTH - 110/WIDTH_6_SCALE, [[self class] userWordLabelHeight:self.model[@"userWord"]]);
    self.userDateLab.frame = CGRectMake(self.userWordLab.left, self.userWordLab.bottom + 20/WIDTH_6_SCALE, 80/WIDTH_6_SCALE, 13/WIDTH_6_SCALE);
    self.grayBGView.frame = CGRectMake(60/WIDTH_6_SCALE, self.userDateLab.bottom + 17/WIDTH_6_SCALE, SCREEN_WIDTH  - 59/WIDTH_6_SCALE - 46/WIDTH_6_SCALE, [[self class] userWordLabelHeight:self.model[@"officialWord"]] + 63/WIDTH_6_SCALE);
    self.officialWordLab.frame = CGRectMake(15/WIDTH_6_SCALE, 18/WIDTH_6_SCALE, self.grayBGView.width - 30/WIDTH_6_SCALE, [[self class] userWordLabelHeight:self.model[@"officialWord"]]);
    self.bottomLineView.frame = CGRectMake(0, self.contentView.bottom - 1/WIDTH_6_SCALE, SCREEN_WIDTH, 1/WIDTH_6_SCALE);
    self.officialFavourBtn.frame = CGRectMake(self.userFavourBtn.left, self.grayBGView.top + 20/WIDTH_6_SCALE, self.userFavourBtn.width, self.userFavourBtn.height);
    self.officialNumLab.frame = CGRectMake(self.userNumberLab.left, self.officialFavourBtn.bottom + 8/WIDTH_6_SCALE, self.userNumberLab.width, self.userNumberLab.height);
    self.officialNumLab.centerX = self.officialFavourBtn.centerX;
    self.greenLineView.frame = CGRectMake(15/WIDTH_6_SCALE, self.officialWordLab.bottom + 15/WIDTH_6_SCALE, 2/WIDTH_6_SCALE, 13/WIDTH_6_SCALE);
    self.officialDateLab.frame = CGRectMake(self.greenLineView.right + 5/WIDTH_6_SCALE, 0, self.grayBGView.width - self.greenLineView.right - 10/WIDTH_6_SCALE, self.greenLineView.height);
    self.officialDateLab.centerY = self.greenLineView.centerY;
}
#pragma mark - action
- (void)officialFavourBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
}
- (void)userFavourBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
}
@end
